<?php
namespace App\Service;
use Illuminate\Support\Facades\Config;
use EasyWeChat\Factory;
/**
 * 微信支付
 */
class WxPay
{
    public function pay($data)
    {
        $options = [
            'app_id' => Config::get(pay . app_id),
            'mch_id' => Config::get(pay . mch_id),
            'key' => Config::get(pay . mch_key),
            'notify_url' => 'https://example.com/notify'
        ];
        $payment = Factory::payment($options);
        $jssdk = $payment->jssdk;
        $attributes = [
            'trade_type' => 'APP',              // 支付方式，app支付使用APP
            'body' => $data['type'],            // 订单说明
            'out_trade_no' =>$data['sn'],  // 自定义订单号
            'total_fee' => $data['price'] * 100,               // 单位：分
            'openid' => $data['id']
        ];
        $result = $payment->order->unify($attributes);
        if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
            $prepayId = $result['prepay_id'];
            $config = $jssdk->sdkConfig($prepayId);
            return response($config);
        }
        if ($result['return_code'] == 'FAIL' && array_key_exists('return_msg', $result)) {
            return $this->responseError(-1, $result['return_msg']);
        }
        return $this->responseError(-1, $result['err_code_des']);
    }

}
